<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @submit.native.prevent="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="二级分类" size="small">
            <el-input v-model="searchForm.secondaryClassification"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="商品名称" size="small">
            <el-input v-model="searchForm.productName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="380px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="80px">
          <el-form-item label="一级分类" size="small">
            <el-input v-model="teamRow.primaryClassification"></el-input>
          </el-form-item>
          <el-form-item label="二级分类" size="small">
            <el-input v-model="teamRow.secondaryClassification"></el-input>
          </el-form-item>
          <!--<el-form-item label="三级分类" size="small">-->
          <!--<el-input v-model="teamRow.threeClassification"></el-input>-->
          <!--</el-form-item>-->
          <el-form-item label="商品名称" size="small">
            <el-input v-model="teamRow.productName"></el-input>
          </el-form-item>
          <el-form-item label="单位" size="small">
            <el-input v-model="teamRow.unit"></el-input>
          </el-form-item>
          <el-form-item label="采购价格" size="small">
            <el-input type="number" v-model.number="teamRow.priceOne">
              <span slot="suffix">元</span>
            </el-input>
          </el-form-item>
          <el-form-item label="显示价格" size="small">
            <el-input type="number" v-model.number="teamRow.priceTwo">
              <span slot="suffix">元</span>
            </el-input>
          </el-form-item>
          <el-form-item prop="remark" label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading">
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="100">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.detailId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    selectPriceDetail,
    addPriceDetail,
    deletePriceDetail,
    getList,
    updatePriceDetail
  } from '@/api/price'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'

  const configs = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    filters: {},
    template: `
  <el-table-column :label="colConfig.label">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :min-width="item.minWidth" :show-overflow-tooltip="true">
                <span slot-scope="scope" >
                    <span>{{scope.row[item.prop]}}</span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'primaryClassification', label: '一级分类', minWidth: '20%' },
          { prop: 'secondaryClassification', label: '二级分类', minWidth: '20%' },
          // { prop: 'threeClassification', label: '三级分类', minWidth: '20%' },
          {
            label: '采购价格', children: [
              { prop: 'productName', label: '商品名称', minWidth: '10%' },
              { prop: 'unit', label: '单位', minWidth: '10%' },
              { prop: 'priceOne', label: '价格', minWidth: '10%' }
            ], component: configs
          },
          {
            label: '显示价格', children: [
              { prop: 'productName', label: '商品名称', minWidth: '10%' },
              { prop: 'unit', label: '单位', minWidth: '10%' },
              { prop: 'priceTwo', label: '价格', minWidth: '10%' }
            ], component: configs
          },
          { prop: 'remark', label: '备注', minWidth: '20%' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增易耗品领用登记',
        dialogType: 0,
        teamRow: {
          openingStock: 0,
          monthlyStorage: 0,
          outMonthStorage: 0
        },
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        isdisabled: false,
        total: 1,
        outerVisible: false,
        pca: pca,
        rules: {},
        visible: false
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      sum() {
        return this.teamRow.openingStock * this.teamRow.monthlyStorage
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.isdisabled = false
        this.outerVisible = true
        this.dialogTitle = '新增价格明细'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
      },
      editTeam(detailId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.isdisabled = true
        this.outerVisible = true
        this.dialogTitle = '修改价格明细'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          detailId: detailId
        }
        console.log(params)
        selectPriceDetail(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = {
          teamId: this.teamRow.teamId,
          primaryClassification: this.teamRow.primaryClassification || '',
          secondaryClassification: this.teamRow.secondaryClassification || '',
          // threeClassification: this.teamRow.threeClassification ? this.teamRow.threeClassification : '',
          productName: this.teamRow.productName || '',
          unit: this.teamRow.unit || '',
          priceOne: this.teamRow.priceOne || '',
          priceTwo: this.teamRow.priceTwo || '',
          remark: this.teamRow.remark || ''
        }
        if (!this.dialogType) {
          params.detailId = this.teamRow.detailId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addPriceDetail(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updatePriceDetail(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        console.log(scope)
        console.log(2020020)
        console.log(this.$refs[`popover-${scope.$index}`])
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          quantity: scope.row.quantity,
          detailId: scope.row.detailId,
          storageId: scope.row.storageId,
          classification: scope.row.classification,
          isDel: 1
        }
        deletePriceDetail(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      addressChange() {
        this.teamRow.provinceId = this.teamRow.addressId[0]
        this.teamRow.cityId = this.teamRow.addressId[1]
        this.teamRow.address = this.pca[86][this.teamRow.addressId[0]] + this.pca[this.teamRow.addressId[0]][this.teamRow.addressId[1]]
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
